var args = undefined;
var totalPage = 0;

var ms = {
    init: function(obj, args){
    	ms.fillHtml(obj, args);
        ms.bindEvent(obj, args);
    },
    reset:function(obj){
    	ms.fillHtml(obj, args);
    },
    //填充html
    fillHtml: function (obj, args) {
    	var current = parseInt($("#currentPage").val(), 10) || 1;
    	if (current > totalPage && totalPage != 0) {
    		current = totalPage;
    		$('#page').val(current);
    	}
        return (function() {
            obj.empty();
            //上一页
            if (current > 1) {
                obj.append('<a href="javascript:;" class="prevPage">上一页</a>');
            } else {
                obj.remove('.prevPage');
                obj.append('<span class="disabled">上一页</span>');
            }
            //中间页码
            if (current != 1 && current >= 4 && totalPage != 4) {
                obj.append('<a href="javascript:;" class="tcdNumber">' + 1 + '</a>');
            }
            if (current - 2 > 2 && current <= totalPage && totalPage > 5) {
                obj.append('<span>...</span>');
            }
            var start = current - 2, end = current + 2;
            if ((start > 1 && current < 4) || current == 1) {
                end++;
            }
            if (current > totalPage - 4 && current >= totalPage) {
                start--;
            }
            for (;start <= end; start++) {
                if (start <= totalPage && start >= 1) {
                    if (start != current) {
                        obj.append('<a href="javascript:;" class="tcdNumber">' + start + '</a>');
                    } else {
                        obj.append('<span class="current">' + start + '</span>');
                    }
                }
            }
            var cnt = parseInt(current, 10) + 2;
            if (cnt < totalPage - 1 && current >= 1 && totalPage > 5) {
                obj.append('<span>...</span>');
            }
            if (current != totalPage && current < totalPage -2 && totalPage != 4) {
                obj.append('<a href="javascript:;" class="tcdNumber">' + totalPage + '</a>');
            }
            //下一页
            if (current < totalPage) {
                obj.append('<a href="javascript:;" class="nextPage">下一页</a>');
            } else {
                obj.remove('.nextPage');
                obj.append('<span class="disabled">下一页</span>');
            }
            //跳转页码
            if (args && args.dispalyInputPage) {
            	if(current <= totalPage && totalPage > 5){
            		obj.append('<span class="inputPage"><input id="currentPage" type="text"><a href="javascript:;" class="turndown">GO</a><span>');
            	} else {
                	obj.remove('.turndown');
                	obj.append('<span class="inputPage" hidden><input id="currentPage" type="text"><a href="javascript:;" class="turndown">GO</a><span>');
            	}
            }
            $("#currentPage").val(current);
        })();
    },
    //绑定事件
    bindEvent: function(obj, args) {
    	var dispalyInputPage = args && args.dispalyInputPage;
        return (function() {
            obj.on("click", "a.tcdNumber", function() {
                var current = parseInt($(this).text(), 10);
                if (typeof(args.backFn) == "function") {
                	$("#currentPage").val(current);
                	$('#page').val(current);
                    args.backFn();
                }
                ms.reset(obj);
            });
            //上一页
            obj.on("click", "a.prevPage", function() {
            	var current = parseInt($(".current").text(), 10);
            	if (current == 1) {
            		showMessge(messageContent["Page.FirstPage"]);
            	} else {
            		if (typeof(args.backFn) == "function") {
            			$("#currentPage").val(current - 1);
            			$('#page').val(current - 1);
                        args.backFn();
                    }
            		ms.reset(obj);
            	}
            });
            //下一页
            obj.on("click", "a.nextPage", function(){
            	var current = parseInt($(".current").text(), 10);
            	if (current == totalPage) {
            		showMessge(messageContent["Page.LastPage"]);
            	} else {
            		if (typeof(args.backFn)=="function") {
            			$("#currentPage").val(current + 1);
            			$('#page').val(current + 1);
                        args.backFn();
                    }
            		ms.reset(obj);
            	}
            });
            //跳转
            obj.on("click", "a.turndown", function() {
            	var current = parseInt($("#currentPage").val(), 10);
                if (current < 1 || current > totalPage) {
                    showMessge(messageContent["Page.OverRange"]);
                } else {
                	if (typeof(args.backFn) == "function") {
                		$("#currentPage").val(current);
                		$('#page').val(current);
                        args.backFn();
                    }
                	ms.reset(obj);
                }
            });
        })();
    }
}
$.fn.createTurnPage = function(options){
    args = $.extend({dispalyInputPage: true, backFn: function(){}}, options);
    ms.init(this, args);
}
$.fn.resetTurnPage = function(){
    ms.reset(this);
}